﻿using System.Collections.Generic;
using System.ServiceModel.Dispatcher;
using UFIDA.U9.WSS.ProxyService.Exceptions;
using UFIDA.U9.WSS.ProxyService.Models;

namespace UFIDA.U9.WSS.ProxyService.Behavior.Endpoint
{
    /// <summary>
    ///     内嵌CorrelationState
    /// </summary>
    public class InnerCorrelationState
    {
        private readonly Dictionary<IDispatchMessageInspector, object> _inspectorCorrelationStateDict =
            new Dictionary<IDispatchMessageInspector, object>();

        public SessionObject SessionObject { get; set; }

        /// <summary>
        ///     添加动作
        /// </summary>
        /// <param name="inspector"></param>
        /// <param name="correlationState"></param>
        public void Add(IDispatchMessageInspector inspector, object correlationState)
        {
            if (_inspectorCorrelationStateDict.ContainsKey(inspector))
                _inspectorCorrelationStateDict[inspector] = correlationState;
            else
            {
                _inspectorCorrelationStateDict.Add(inspector, correlationState);
            }
        }

        /// <summary>
        ///     获取动作的状态
        /// </summary>
        /// <param name="inspector"></param>
        /// <returns></returns>
        public object GetCorrelationState(IDispatchMessageInspector inspector)
        {
            if (!_inspectorCorrelationStateDict.ContainsKey(inspector))
                throw new WSException(ExceptionCodes.ServiceConfigurationException,
                    "context behavior inner inspector correlationState does not exist");
            return _inspectorCorrelationStateDict[inspector];
        }
    }
}